package tech.heptagram.staff.api.v3;

import com.github.pagehelper.PageInfo;

import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import tech.heptagram.core.common.rest.ResponseResult;
import tech.heptagram.core.common.util.JsonUtil;
import tech.heptagram.staff.model.Staff;
import tech.heptagram.staff.model.StaffAuthException;
import tech.heptagram.staff.query.StaffQuery;
import tech.heptagram.staff.query.StaffRemoveReq;
import tech.heptagram.staff.query.StaffSaveReq;
import tech.heptagram.staff.service.StaffService;

/**
 * @author wang.yuchuan
 * @date 2018/8/30
 */
@Api("员工相关api")
@RestController
@RequestMapping("/api/v3/staff")
@Slf4j
public class StaffApiRestController implements StaffApi {

    @Resource
    private StaffService staffService;

    @ApiOperation("保存新员工接口")
    @PostMapping("save")
    @Override
    public ResponseResult<String> save(@RequestBody StaffSaveReq staffSaveReq) {
        log.info("保存员工信息 save 接收参数：staffSaveReq:{}",staffSaveReq);
        String code = staffService.saveStaff(staffSaveReq);
        log.info("保存员工信息 save 返回结果：staffCode:{}",code);
        return new ResponseResult<>(code);
    }

    @ApiOperation("修改员工接口")
    @PostMapping("update")
    @Override
    public ResponseResult update(@RequestBody StaffSaveReq updateStaffReq) {
        log.info("修改员工信息 update 接收参数：updateStaffReq:{}",updateStaffReq);
        staffService.updateStaff(updateStaffReq);
        log.info("修改员工信息 update 执行成功");
        return new ResponseResult();
    }

    @ApiOperation("员工账号身份认证接口")
    @PostMapping("auth/{account}/{password}")
    @Override
    public ResponseResult<Staff> auth(@PathVariable String account, @PathVariable String password) throws StaffAuthException {
        log.info("员工身份认证 auth 接受参数:account {} password:{} ", account, password);
        Staff staff = staffService.auth(account, password);
        log.info("员工身份认证 auth 返回结果:{}", staff);
        return new ResponseResult<>(staff);
    }

    @ApiOperation("更新员工花名接口")
    @PostMapping("modify/nickname")
    @Override
    public ResponseResult updateStaffNickname(@RequestBody Staff staff) {
        log.info("更新员工花名 updateStaffNickname 接口参数: staffCode:{} nickName:{}", staff.getCode(), staff.getNickName());
        staffService.updateStaffNickname(staff.getCode(), staff.getNickName());
        log.info("更新员工花名 updateStaffNickname 接口执行成功");
        return new ResponseResult();
    }

    @ApiOperation("分页查询所有符合条件的员工列表")
    @PostMapping("listpage")
    @Override
    public ResponseResult<PageInfo<Staff>> findStaffsWithPage(@RequestBody StaffQuery staffQuery) {
        log.info("查询员工信息 findStaffsWithPage 接受参数:{}", staffQuery.toString());
        PageInfo<Staff> staffs = staffService.findStaffsWithPage(staffQuery);
        log.info("查询员工信息 findStaffsWithPage 返回结果:{}", JsonUtil.toJson(staffs));

        return new ResponseResult<>(staffs);
    }

    @ApiOperation("根据查询条件查询所有符合条件的员工列表")
    @PostMapping("list")
    @Override
    public ResponseResult<List<Staff>> findStaffs(@RequestBody StaffQuery staffQuery) {
        log.info("查询员工信息 findStaffs 接受参数:{}", staffQuery.toString());
        List<Staff> staffs = staffService.findStaffs(staffQuery);
        log.info("查询员工信息 findStaffs 返回结果:{}", JsonUtil.toJson(staffs));

        return new ResponseResult<>(staffs);
    }

    @ApiOperation("根据查询条件获取指定员工")
    @PostMapping("get")
    @Override
    public ResponseResult<Staff> getStaff(@RequestBody StaffQuery staffQuery) {
        log.info("查询员工信息 getStaff 接受参数:{}", staffQuery.toString());
        Staff staff = staffService.getStaff(staffQuery);
        log.info("查询员工信息 getStaff 返回结果:{}", Objects.isNull(staff) ? "null" : staff.toString());
        return new ResponseResult<>(staff);
    }

    @ApiOperation("根据staffCode获取指定员工")
    @GetMapping("get/code/{staffCode}")
    @Override
    public ResponseResult<Staff> getByCode(@PathVariable String staffCode) {
        log.info("查询员工信息 getByCode 接受参数:{}", staffCode);
        Staff staff = staffService.getByCode(staffCode);
        log.info("查询员工信息 getByCode 返回结果:{}", Objects.isNull(staff) ? "null" : staff.toString());
        return new ResponseResult<>(staff);
    }

    @ApiOperation("根据staffName获取指定员工, 支持真名、花名、拼音")
    @GetMapping("get/name/{staffName}")
    @Override
    public ResponseResult<Staff> getByName(@PathVariable String staffName) {
        log.info("查询员工信息 getByName 接受参数:{}", staffName);
        Staff staff = staffService.getByName(staffName);
        log.info("查询员工信息 getByName 返回结果:{}", Objects.isNull(staff) ? "null" : staff.toString());
        return new ResponseResult<>(staff);
    }

    @ApiOperation("根据mobile获取指定员工")
    @GetMapping("get/mobile/{mobile}")
    @Override
    public ResponseResult<Staff> getByMobile(@PathVariable String mobile) {
        log.info("查询员工信息 getByMobile 接受参数:{}", mobile);
        Staff staff = staffService.getByMobile(mobile);
        log.info("查询员工信息 getByMobile 返回结果:{}", Objects.isNull(staff) ? "null" : staff.toString());
        return new ResponseResult<>(staff);
    }

    @ApiOperation("根据wxUserId获取指定员工")
    @GetMapping("get/wxid/{wxUserId}")
    @Override
    public ResponseResult<Staff> getByWxUserId(@PathVariable String wxUserId) {
        log.info("查询员工信息 getByWxUserId 接受参数:{}", wxUserId);
        Staff staff = staffService.getByWxUserId(wxUserId);
        log.info("查询员工信息 getByWxUserId 返回结果:{}", Objects.isNull(staff) ? "null" : staff.toString());
        return new ResponseResult<>(staff);
    }

    @ApiOperation("根据wxUserId获取指定员工")
    @GetMapping("get/dingtalk/{dingtalkId}")
    @Override
    public ResponseResult<Staff> getByDingtalkId(@PathVariable String dingtalkId) {
        log.info("查询员工信息 getByDingtalkId 接受参数:{}", dingtalkId);
        Staff staff = staffService.getByDingtalkId(dingtalkId);
        log.info("查询员工信息 getByDingtalkId 返回结果:{}", Objects.isNull(staff) ? "null" : staff.toString());
        return new ResponseResult<>(staff);
    }

    @ApiOperation("根据nickName获取指定员工")
    @GetMapping("get/nickname/{nickName}")
    @Override
    public ResponseResult<Staff> getByNickName(@PathVariable String nickName) {
        log.info("查询员工信息 getByNickName 接受参数:{}", nickName);
        Staff staff = staffService.getByNickName(nickName);
        log.info("查询员工信息 getByNickName 返回结果:{}", Objects.isNull(staff) ? "null" : staff.toString());
        return new ResponseResult<>(staff);
    }

    @ApiOperation("更新员工状态")
    @PostMapping("update/status")
    @Override
    public ResponseResult updateStatus(@RequestBody StaffRemoveReq req) {
        log.info("更新员工状态 updateStatus 接受参数：{}",req);
        staffService.updateStaffStatus(req);
        log.info("更新员工状态 执行成功");
        return new ResponseResult();
    }

    /**
     * 根据部门ID查询员工列表
     *
     * @param departmentId 部门ID
     * @return 员工列表
     */
    @Override
    @ApiOperation("根据部门ID获取指定员工")
    @GetMapping("/get/departmentId/{departmentId}")
    public ResponseResult<List<Staff>> getByDepartmentId(@PathVariable Integer departmentId) {
        log.info("查询员工信息 getByDepartmentId 接受参数:{}", departmentId);
        List<Staff> list = staffService.getByDepartmentId(departmentId);
        log.info("查询员工信息 getByDepartmentId 返回结果:{}", list);
        return new ResponseResult<>(list);
    }

    /**
     * 根据部门ID列表查询员工CODE列表
     *
     * @param deptIdList 部门ID列表
     * @return 员工CODE列表
     */
    @Override
    @ApiOperation("根据部门ID列表获取员工编码列表")
    @PostMapping("/get/staffCodeList")
    public ResponseResult<List<String>> getStaffCodeListByDeptIdList(@RequestBody Integer[] deptIdList) {
        List<String> list = staffService.getStaffCodeListByDeptIdList(Arrays.asList(deptIdList));
        return new ResponseResult<>(list);
    }
}
